﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Models.Admin;
using Gfw.Repositories.Admin;
using Gfw.Repository.Admin;
using L.Basic.Authority.Context;
using L.Basic.Authority.Models;
using L.Basic.Authority.Services;

namespace Gfw.BLL.Basic.User
{
    public class SystemUserAuthentication : IAuthenticationService
    {
        public L.Basic.Authority.Models.UserLogin GetLoginInfo(string userId)
        {
            var rep = new UsrUserLoginRepository();
            var permUserLogin = rep.Get(userId);
            if (permUserLogin == null)
                return null;
            UserLogin result = new UserLogin()
                                   {
                                       Userid = permUserLogin.Userid,
                                       UserOnline = permUserLogin.IsUserOnline,
                                       LastLoginIp = permUserLogin.LastLoginip,
                                       LastLoginTime = permUserLogin.LastLoginDate,
                                       LoginIp = permUserLogin.Loginip,
                                       // LoginName = permUserLogin.,
                                       LoginTime = permUserLogin.LoginDate
                                   };
            return result;
        }

        public void UpdateLoginInfo(L.Basic.Authority.Models.UserLogin userLogin, string loginIp, DateTime loginTime)
        {
            userLogin.LastLoginIp = userLogin.LoginIp;
            userLogin.LastLoginTime = userLogin.LoginTime;

            userLogin.LoginIp = loginIp;
            userLogin.LoginTime = loginTime;
            var rep = new UsrUserLoginRepository();

            UsrUserlogin permUserLogin = new UsrUserlogin()
                                             {
                                                 LastLoginip = userLogin.LastLoginIp,
                                                 LastLoginDate = userLogin.LastLoginTime,
                                                 Loginip = userLogin.LoginIp,
                                                 // LoginName = userLogin.LoginName,
                                                 LoginDate = userLogin.LoginTime,
                                                 Userid = userLogin.Userid,
                                                 IsUserOnline = userLogin.UserOnline
                                             };
            rep.SaveOrUpdate(permUserLogin);
        }

        public void UpdateLoginInfo(string userId, string loginIp, DateTime loginTime)
        {
            var userLogin = GetLoginInfo(userId);
            UpdateLoginInfo(userLogin, loginIp, loginTime);
        }

        public bool Login(string loginName, string password, out L.Basic.Authority.Context.Context context)
        {
            context = null;
            L.Basic.Authority.Models.User user;
            var rep = new SystemUserRepository();
            if (rep.Validate(loginName, password, out user))
            {
                context = new Context()
                              {
                                  CurrentUser = user
                              };
                //获取权限信息
                // context.Permissions = GetModulePermission(user.Roles);
                return true;
            }
            return false;
        }

        public IList<L.Basic.Authority.Models.ModulePermission> GetModulePermission(
            IList<L.Basic.Authority.Models.Role> roles)
        {
            throw new NotImplementedException();
        }

        public void Logout()
        {
            throw new NotImplementedException();
        }

        public bool ChangePassword(string loginName, string oldPassword, string newPassword)
        {
            SystemUserRepository repository = new SystemUserRepository();
            return repository.ChangePassword(loginName, oldPassword, newPassword);
        }
    }
}

